package com.demo03.test01;

import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LocKTest1 {
    public static void main(String[] args) {
        // Executors.newFixedThreadPool(3);
        Business business = new Business();
        //ExecutorService threadpool = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), new ThreadPoolExecutor.AbortPolicy());
        ExecutorService threadpool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            threadpool.execute(new Runnable() {
                @Override
                public void run() {
                    business.service();
                }
            });
        }
        threadpool.shutdown();
    }

    static class Business {
        private int count = 0;
        private Lock lock = new ReentrantLock();

        public /*synchronized*/ void service() {
            lock.lock();
            try {
                count++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(count);
            } catch (RuntimeException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
